<template>
  <div class="rolePage">
    <el-dialog title="选择司机" :visible.sync="dialogVisible" :append-to-body="true" width="70%">
      <el-row>
        <el-col style="text-align: center;">
          <el-input
            v-model.trim="keywords"
            placeholder="输入司机名称"
            :style="'margin:5px 0px;'"
            @keyup.enter.native="search"
          >
            <el-button slot="append" @click="search">搜索</el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-row>
        <el-col :sm="8">
          <div class="g-title">司机数量({{total}}个）</div>
        </el-col>
      </el-row>
      <el-table
        ref="multipleTable"
        :data="listData"
        tooltip-effect="dark"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="trueName" label="姓名" show-overflow-tooltip align="center"></el-table-column>
        <el-table-column prop="handSet" label="手机号" show-overflow-tooltip align="center"></el-table-column>
        <el-table-column prop="drivingType" label="驾照类型" align="center"></el-table-column>
        <!-- <el-table-column prop="sex" label="性别" align="center">
          <template slot-scope="scope">
            <div>{{scope.row.sex==0?'男':'女'}}</div>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status==0" type="primary" size="mini" :disabled="true">出车中</el-button>
            <el-button
              type="primary"
              v-else
              @click="select(scope.row)"
              size="mini"
              :disabled="selectedIds.indexOf(scope.row.id)!=-1"
            >{{selectedIds.indexOf(scope.row.id)!=-1?"已选":"选择"}}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-row style="padding: 32px 16px;">
        <el-col>
          <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :page-size="pageSize"
            :total="total"
            :current-page="pageNo"
            @current-change="fetchData"
            @size-change="pageSizeChange"
          ></el-pagination>
        </el-col>
      </el-row>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listJson as carListJson } from "@/api/car";
import { listJson as driverListJson } from "@/api/driver";
export default {
  data: function() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      listData: [], //全部数据
      listDataSelected: [], //选中数据
      dialogVisible: false,
      parentIndex: 0, //父组件的司机index，选择好了后修改父组件的值
      selectedIds: [], //父组件已选的值 按钮disabled不让再选
      keywords: "" //搜索
    };
  },
  created() {},
  methods: {
    //搜索
    search() {
      this.pageNo = 1;
      this.fetchData();
    },
    //父组件调用 显示弹窗
    show(parentIndex, selectedIds) {
      this.keywords = "";
      this.pageNo = 1;
      this.parentIndex = parentIndex;
      this.selectedIds = selectedIds;
      this.fetchData();
      this.dialogVisible = true;
    },
    //选择 关闭弹窗 通知父组件选择的值
    select(data) {
      this.$parent.formData.usecaritems[this.parentIndex].driverId = data.id;
      this.$parent.formData.usecaritems[this.parentIndex].driverName =
        data.trueName;
      this.dialogVisible = false;
    },
    pageSizeChange(newSize) {
      this.pageSize = newSize;
      this.pageNo = 1;
      this.fetchData();
    },
    async fetchData(pageNo) {
      if (pageNo) {
        this.pageNo = pageNo;
      }
      let params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        use:0
      };
      if (this.keywords) {
        params.username = this.keywords;
      }
      let data = await driverListJson(params);
      this.listData = data.data;
      this.total = data.total;
      this.listDataSelected.splice(0);
    },
    //表格多选
    handleSelectionChange(val) {
      this.listDataSelected.splice(0);
      val.forEach(e => {
        this.listDataSelected.push(e);
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.g-title {
  height: 20px;
  padding-left: 8px;
  line-height: 22px;
  font-size: 16px;
  font-weight: 400;
  border-left: 2px solid #f54e40;
  margin: 10px 0;
  display: inline-block;
}
</style>